Setup nginx repo:
  file.managed:
    - name: /etc/yum.repos.d/nginx.repo
    - source: salt://nginx/files/nginx.repo

Setup nginx:
  group.present:
    - name: nginx
    - gid: 99999

  user.present:
    - name: nginx
    - shell: /sbin/nologin
    - uid: 99999
    - gid: 99999
    - unique: false

  pkg.installed:
    - pkgs:
      - nginx


Copy default nginx config nginx.conf:
  file.managed:
    - name: /etc/nginx/nginx.conf
    - source: salt://nginx/files/nginx.conf.jinja
    - template: jinja


{% for vhosts_conf in salt['pillar.get']('vhost', {}) %}
Generate {{ vhosts_conf }} vhost config for nginx:
  file.managed:
    - name: /etc/nginx/conf.d/{{ vhosts_conf }}.conf
    - source: salt://nginx/files/conf.d/vhost.conf.jinja
    - template: jinja
    - context:
        app_name: {{ vhosts_conf }}
    - listen_in:
      - service: nginx
{% endfor %}

{% if 1 == 2 %}
Prepare Directory for Self Signed SSL Certificate:
  file.directory:
    - name: /etc/lighttpd/ssl/jobst.jobstreet.com
    - makedirs: True

Copy Self Signed Certificate:
  file.managed:
    - name: /etc/lighttpd/ssl/jobst.jobstreet.com/server.pem
    - source: salt://lighttpd/files/server.pem
    - template: jinja
    - listen_in:
      - service: lighttpd

Copy support related files to /var/www/support:
  file.recurse:
    - name: /var/www/support
    - source: salt://lighttpd/files/support
    - dir_mode: 755
    - file_mode: 644
{% endif %}

Setup nginx logrotate script:
  file.managed:
    - name: /etc/logrotate.d/nginx
    - source: salt://nginx/files/logrotate-nginx
    - template: jinja

#This will not throw error even if lighttpd does no exist
Disable and Stop lighttpd:
  service.dead:
    - name: lighttpd
    - enable: False

Enable nginx service:
  service.running:
    - name: nginx
    - enable: True
    - listen:
      - file: /etc/nginx/nginx.conf

